const tagViews = {
  state: {
    visitedViews: [],
    cachedViews: []
  },
  mutations: {
    ADD_VISITED_VIEWS(state, view) {
      if (state.visitedViews.some(v => v.path === view.path)) {
        return
      }
      state.visitedViews.push({
        path: view.path,
        title: view.meta.title
      })
    },
    CLOSE_VISITED_VIEWS(state, route) {
      state.visitedViews = state.visitedViews.filter(
        item => item.path !== route.path
      )
    }
  },
  actions: {
    addVisitedViews({ commit }, view) {
      commit('ADD_VISITED_VIEWS', view)
    },
    closeVisitedViews({ commit, state }, route) {
      return new Promise(resolve => {
        commit('CLOSE_VISITED_VIEWS', route)
        resolve([...state.visitedViews])
      })
    }
  }
}

export default tagViews
